﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Serialization;

namespace xml解析
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            string xml =
                "  <xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[发放成功]]></return_msg><result_code><![CDATA[SUCCESS]]></result_code><mch_billno><![CDATA[1272570601201601283659167720]]></mch_billno><mch_id>1272570601</mch_id><wxappid><![CDATA[wx2a46c1e2d39c43fa]]></wxappid><re_openid><![CDATA[ohAgywhoLODjugjwSQhDwZeC6T5s]]></re_openid><total_amount>100</total_amount><send_listid><![CDATA[0010609753201601280463122315]]></send_listid><send_time><![CDATA[20160128114545]]></send_time></xml>";

         xml=   xml.Replace("xml", "WeChatRedEnvelopeResult");
         WeChatRedEnvelopeResult person2 = Deserialize(typeof(WeChatRedEnvelopeResult), xml) as WeChatRedEnvelopeResult;
            #region MyRegion
            SortedDictionary<string, string> requestXML = GetInfoFromXml(xml);

          
            string dd = requestXML["ComponentVerifyTicket"];
            foreach (KeyValuePair<string, string> k in requestXML)
            {
                if (k.Key == "prepay_id")
                {
                    //  prepay_id = k.Value;
                    break;
                }
            }
            #endregion



        }
        public static object Deserialize(Type type, string xml)
        {
            using (StringReader sr = new StringReader(xml))
            {
                XmlSerializer xmldes = new XmlSerializer(type);
                return xmldes.Deserialize(sr);
            }
        }  
        /// <summary>
        /// 把XML数据转换为SortedDictionary<string, string>集合
        /// </summary>
        /// <param name="strxml"></param>
        /// <returns></returns>
        public static SortedDictionary<string, string> GetInfoFromXml(string xmlstring)
        {
            SortedDictionary<string, string> sParams = new SortedDictionary<string, string>();
            try
            {
                XmlDocument doc = new XmlDocument();
                doc.LoadXml(xmlstring);
                XmlElement root = doc.DocumentElement;
                int len = root.ChildNodes.Count;
                for (int i = 0; i < len; i++)
                {
                    string name = root.ChildNodes[i].Name;
                    if (!sParams.ContainsKey(name))
                    {
                        sParams.Add(name.Trim(), root.ChildNodes[i].InnerText.Trim());
                    }
                }
            }
            catch (Exception ex)
            {
                //LxCommomHelper.Commom.TraceLog.LogWrite(ex.ToString(), LxCommomHelper.Commom.LogEnum.Pay);
            }
            return sParams;
        }



        /// <summary>
        /// 反序列化
        /// </summary>
        /// <param name="xml">XML字符串</param>
        /// <returns></returns>
        /// <summary>
        /// XML String 反序列化成对象
        /// </summary>
        public static T XmlDeserialize<T>(string xmlString)
        {
            T t = default(T);
            XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));
            using (Stream xmlStream = new MemoryStream(Encoding.UTF8.GetBytes(xmlString)))
            {
                using (XmlReader xmlReader = XmlReader.Create(xmlStream))
                {
                    Object obj = xmlSerializer.Deserialize(xmlReader);
                    t = (T)obj;
                }
            }
            return t;
        }




    }
    [Serializable]
    public class class1
    {
        public string AppId { get; set; }
        public string ComponentVerifyTicket { get; set; }

        public string InfoType { get; set; }
        public string CreateTime { get; set; }
    }
}
